
package rentalgui;

import database.ProviderDao;
import database.ServiceDao;
import javax.swing.JOptionPane;
import rentaldb.Provider;

public class ProviderInputWindow extends javax.swing.JFrame {

    /**
     * Creates new form ExpenseInputWindow
     */
    public ProviderInputWindow(ServiceDao sdao, ProviderDao provdao) {
        initComponents();
        this.sdao = sdao;
        this.provdao = provdao;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel2 = new javax.swing.JLabel();
        inputName = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        buttonClear = new javax.swing.JButton();
        buttonEnter = new javax.swing.JButton();
        buttonClose = new javax.swing.JButton();
        inputEmail = new javax.swing.JTextField();
        inputOfficePhone = new javax.swing.JTextField();
        inputMobilePhone = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        inputFax = new javax.swing.JTextField();
        inputAddress = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        inputZip = new javax.swing.JTextField();
        inputState = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Provider Input");
        setResizable(false);

        jLabel2.setText("Name");

        jLabel3.setText("Email");

        jLabel4.setText("Office Phone");

        jLabel5.setText("Mobile Phone");

        buttonClear.setText("Clear");
        buttonClear.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonClearActionPerformed(evt);
            }
        });

        buttonEnter.setText("Enter Data");
        buttonEnter.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonEnterActionPerformed(evt);
            }
        });

        buttonClose.setText("Close Window");
        buttonClose.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonCloseActionPerformed(evt);
            }
        });

        jLabel7.setText("Fax");

        jLabel8.setText("Address");

        jLabel6.setText("State");

        jLabel9.setText("Zip");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(buttonClear, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(buttonEnter, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(buttonClose))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(15, 15, 15)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3)
                            .addComponent(jLabel5)
                            .addComponent(jLabel7)
                            .addComponent(jLabel8)
                            .addComponent(jLabel4)
                            .addComponent(jLabel6)
                            .addComponent(jLabel9))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(inputZip, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(inputState, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(inputAddress, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(inputFax, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(inputName, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(inputEmail)
                            .addComponent(inputOfficePhone)
                            .addComponent(inputMobilePhone))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(inputName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(inputEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(inputOfficePhone, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(inputMobilePhone, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(inputFax, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(inputAddress, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(inputState, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(inputZip, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel9))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(buttonEnter)
                    .addComponent(buttonClear)
                    .addComponent(buttonClose))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void buttonClearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonClearActionPerformed
        inputName.setText("");
        inputEmail.setText("");
        inputOfficePhone.setText("");
        inputMobilePhone.setText("");
        inputFax.setText("");
        inputAddress.setText("");
        inputState.setText("");
        inputZip.setText("");
    }//GEN-LAST:event_buttonClearActionPerformed

    private void buttonEnterActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonEnterActionPerformed
        if(allFieldsAreFull()){
            try{
                String name = inputName.getText().trim();
                String email = inputEmail.getText().trim();
                String officephone = inputOfficePhone.getText().trim();
                String mobilephone = inputMobilePhone.getText().trim();
                String fax = inputFax.getText().trim();
                String address = inputAddress.getText().trim();
                String state = inputState.getText().trim();
                Integer zip = Integer.parseInt(inputZip.getText().trim());
                
                Provider provider = new Provider();
                provider.setName(name);
                provider.setEmail(email);
                provider.setOfficephone(officephone);
                provider.setMobilephone(mobilephone);
                provider.setFax(fax);
                provider.setAddress(address);
                provider.setState(state);
                provider.setZip(zip);
                provdao.persist(provider);
                JOptionPane.showMessageDialog(null, "Input successfully added!", "Input Successful", JOptionPane.INFORMATION_MESSAGE);
            }
            catch (Exception e){
                JOptionPane.showMessageDialog(null, "Not all input processed correctly.", "Input Error", JOptionPane.ERROR_MESSAGE);
            }
        } else{
            JOptionPane.showMessageDialog(this, "All fields must be full before submitting.", "ALERT", JOptionPane.WARNING_MESSAGE);
        }
    }//GEN-LAST:event_buttonEnterActionPerformed

    private void buttonCloseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonCloseActionPerformed
        dispose();
    }//GEN-LAST:event_buttonCloseActionPerformed

    private boolean allFieldsAreFull(){
        return !inputName.getText().trim().isEmpty()
                && !inputEmail.getText().trim().isEmpty()
                && !inputOfficePhone.getText().trim().isEmpty()
                && !inputMobilePhone.getText().trim().isEmpty()
                && !inputFax.getText().trim().isEmpty()
                && !inputAddress.getText().trim().isEmpty()
                && !inputState.getText().trim().isEmpty()
                && !inputZip.getText().trim().isEmpty();                
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonClear;
    private javax.swing.JButton buttonClose;
    private javax.swing.JButton buttonEnter;
    private javax.swing.JTextField inputAddress;
    private javax.swing.JTextField inputEmail;
    private javax.swing.JTextField inputFax;
    private javax.swing.JTextField inputMobilePhone;
    private javax.swing.JTextField inputName;
    private javax.swing.JTextField inputOfficePhone;
    private javax.swing.JTextField inputState;
    private javax.swing.JTextField inputZip;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    // End of variables declaration//GEN-END:variables
    private ServiceDao sdao; 
    private ProviderDao provdao;
}
